package com.example.controller;

import com.example.api.Result;
import com.example.configura.LogOperation;
import com.example.pojo.Dept;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService  deptService;
    @GetMapping
    public Result<List<Dept>> getAllDept(){

        return Result.success(deptService.getAll());
    }

    @DeleteMapping
    @LogOperation
    public Result<String> deleteById(@RequestParam("id") Integer id){
        if (deptService.deleteDeptById(id)) {
            return Result.success(null);
        }
        return Result.error(0,"删除失败,该部门下关联有员工！");
    }

    @PostMapping
    @LogOperation
    public Result<String> addDept(@RequestBody Dept dept){
        System.out.println("添加部门："+dept);
        if (deptService.addDept(dept)) {
            return Result.success(null);
        }
        return Result.error(1,"添加失败");
    }

    @GetMapping("/{id}")
    public Result<Dept> getDeptById(@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping
    @LogOperation
    public Result<String> updateDept(@RequestBody Dept dept){
        if (deptService.updateDept(dept)) {
            return Result.success(null);
        }
        return Result.error(1,"更新失败");
    }
}
